package shuimin.weixin.message;

import shuimin.weixin.Config;
import shuimin.weixin.util.SHA1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by sky on 16/5/19.
 */
public class ServiceVerify {

    public static boolean verify(String appid, String signature, String timestamp, String nonce, String echostr) {
        //是微信发送过来的验证信息,判断验证并返回
        List<String> params = new ArrayList<>();
        params.add(Config.getToken(appid));
        params.add(timestamp);
        params.add(nonce);
        // 1. 将token、timestamp、nonce三个参数进行字典序排序
        Collections.sort(params, String::compareTo);
        // 2. 将三个参数字符串拼接成一个字符串进行sha1加密
        String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));
        return temp.equals(signature);
    }

}
